<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use app\admin\model\RouteModel;
use app\admin\service\AddressService;
use think\Model;
use think\Db;

class FreightModel extends Model
{
    function get_freight($param){
        $where['tpl_id']=$param['id'];
        $freight_list=$this->where($where)->select()->toArray();
        foreach ($freight_list as $k => $v) {
            if($v['is_whole']==1){
                $address =new AddressService();
                $freight_list[$k]['address_list'] = $address->getAddressListById($v['province_id_array'], $v['city_id_array']);
            }
        }
        return $freight_list;
    }

    function get_good_fright($good_id,$num,$district=0){
        if($district){
            $goods=new GoodsModel();
            $freight= new FreightModel();
            $good=$goods->where('id', $good_id)->find();
            $fright_info=$freight->where("FIND_IN_SET( " . $district . ", district_id_array) and tpl_id=".$good['shipping_tpl']."")->select()->toArray();
            if($fright_info==null){
                $fright_info=$freight->where("is_whole=0 and tpl_id=".$good['shipping_tpl']."")->select()->toArray();
            }
            $fright_info=$fright_info[0];
            $fright=0;
            if($good['shipping_fee_type']==1){
                //重量
                $weight=$good['goods_weight']*$num;
                if($weight<=$fright_info['weight_snum']){
                    $fright=$fright_info['weight_sprice'];
                }else{
                    $c_weight=$weight-$fright_info['weight_snum'];
                    $level=floor($c_weight/$fright_info['weight_xnum']);
                    $fright=$fright_info['weight_sprice']+$level*$fright_info['weight_xprice'];
                }

            }
            if($good['shipping_fee_type']==2){
                //体积
                $volume=$good['goods_volume']*$num;
                if($volume<=$fright_info['volume_snum']){
                    $fright=$fright_info['volume_sprice'];
                }else{
                    $c_weight=$volume-$fright_info['volume_snum'];
                    $level=floor($c_weight/$fright_info['volume_xnum']);
                    $fright=$fright_info['volume_sprice']+$level*$fright_info['volume_xprice'];
                }
            }
            if($good['shipping_fee_type']==3){
                //计件
                if($num<=$fright_info['bynum_snum']){
                    $fright=$fright_info['bynum_sprice'];
                }else{
                    $c_bynum=$num-$fright_info['bynum_snum'];
                    $level=floor($c_bynum/$fright_info['bynum_xnum']);
                    $fright=$fright_info['bynum_sprice']+$level*$fright_info['bynum_xprice'];
                }
            }
            if($good['shipping_fee_type']==4){
                //固定价格
                $fright=$good['shipping_fee'];
            }
            return $fright;
        }else{
            return 0;
        }
    }
}
